using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Common.Pool
{
    public class ObjectPoolManager
    {
        private Dictionary<string, ObjectPoolBase> PoolDic;
        public ObjectPoolManager()
        {
            PoolDic = new Dictionary<string, ObjectPoolBase>();
        }
        public ObjectPoolBase CreatePool<T>(PoolObject<T> obj, string poolName) where T : class
        {
            string key = typeof(T).FullName + poolName;
            if (PoolDic.ContainsKey(key))
            {
                Debug.LogWarning("thePool of the type:" + typeof(T).FullName + "  has already exist");
                return PoolDic[key];
            }
            ObjectPoolBase pool = ObjectPool<T>.Creat(obj);
            PoolDic.Add(key, pool);
            return pool;
        }
        public ObjectPoolBase GetPool<T>(string poolName)
        {
            return InternalGetPool<T>(poolName);
        }
        public void DestoryPool<T>(string poolName)
        {
            var pool = InternalGetPool<T>(poolName);
            if (pool == null)
            {
                Debug.LogWarning("thePool of the type:" + typeof(T).FullName + "  has not exist");
                return;
            }
            pool.Destory();
            string key = typeof(T).FullName + poolName;
            PoolDic.Remove(key);
        }
        public void DestoryAllPool()
        {
            foreach (var item in PoolDic)
            {
                item.Value.Destory();
            }
            PoolDic.Clear();
        }
        public void ReleaseUnUse<T>(string poolName)
        {
            var pool = InternalGetPool<T>(poolName);
            pool.ReleaseUnUsed();
        }
        public void ReleaseAllUnUse()
        {
            foreach (var item in PoolDic)
            {
                item.Value.ReleaseUnUsed();
            }
        }
        private ObjectPoolBase InternalGetPool<T>(string poolName)
        {

            var key = typeof(T).FullName + poolName;
            if (!PoolDic.ContainsKey(key))
            {
                return null;
            }
            return PoolDic[key];
        }

    }
}
